package cn.com.hatech.blog.module.admin.dashboard.service.impl;

import cn.com.hatech.blog.module.admin.article.entity.Article;
import cn.com.hatech.blog.module.admin.comment.entity.Comment;
import cn.com.hatech.blog.module.admin.article.mapper.ArticleMapper;
import cn.com.hatech.blog.module.admin.attach.mapper.AttachMapper;
import cn.com.hatech.blog.module.admin.comment.mapper.CommentMapper;
import cn.com.hatech.blog.module.admin.dashboard.service.ISiteService;
import cn.com.hatech.blog.module.admin.article.vo.ArchiveVO;
import cn.com.hatech.blog.module.admin.dashboard.vo.StatisticsVO;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.blog.module.admin.service.impl
 * @className: SiteServiceImpl
 * @author: xuruikai
 * @description: 站点服务业务层接口实现
 * @date: 2020/1/11 14:27
 * @version: 1.0
 */
@Slf4j
@Service("siteService")
public class SiteServiceImpl implements ISiteService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private ArticleMapper contentMapper;

    @Resource
    private AttachMapper attachMapper;

    @Override
    public List<Comment> recentComments(int limit) {
        log.debug("Enter recentComments method:limit={}", limit);
        if (limit < 0 || limit > 10) {
            limit = 10;
        }
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        String orderByCol = "create_time";
        commentQueryWrapper.orderByDesc(orderByCol);
        log.debug("Exit recentComments method");
        IPage<Comment> list = commentMapper.selectPage(new Page<>(1, limit), commentQueryWrapper);
        return commentMapper.selectList(commentQueryWrapper);
    }

    @Override
    public List<Article> recentContents(int limit) {
        log.debug("Enter recentContents method");
        if (limit < 0 || limit > 10) {
            limit = 10;
        }
        QueryWrapper<Article> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.orderByDesc("create_time");
        IPage<Article> list = contentMapper.selectPage(new Page<>(1, limit), contentQueryWrapper);
        log.debug("Exit recentContents method");
        return list.getRecords();
    }

    @Override
    public Comment getComment(Integer coid) {
        if (null != coid) {
            return commentMapper.selectById(coid);
        }
        return null;
    }

    @Override
    public StatisticsVO getStatistics() {
        log.debug("Enter getStatistics method");
        StatisticsVO statistics = new StatisticsVO();
        QueryWrapper<Article> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.lambda().eq(Article::getStatus, 1);

        Integer articles = contentMapper.selectCount(contentQueryWrapper);

        Integer comments = commentMapper.selectCount(null);

        Integer attaches = attachMapper.selectCount(null);

        statistics.setArticles(articles);
        statistics.setComments(comments);
        statistics.setAttachs(attaches);
        log.debug("Exit getStatistics method");
        return statistics;
    }

    @Override
    public List<ArchiveVO> getArchives() {
        log.debug("Enter getArchives method");
        QueryWrapper<Article> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.lambda().eq(Article::getStatus, 1);
        List<Article> contents = contentMapper.selectList(contentQueryWrapper);
        // 利用lambda表达式根据创建时间分组 ,然后处理retreatedMap 为 ArchiveVO
        List<ArchiveVO> archives = contents.stream()
                // 根据时间进行分组 , 转换为TreeMap
                .collect(Collectors.groupingBy(Article::getCreateTime, TreeMap::new, Collectors.toList()))
                // 时间(key)倒序排列
                .descendingMap()
                // 返回map对应保存键值对的set集合
                .entrySet()
                .stream()
                //  将TreeMap便利转化为ArchiveVO
                .map(retreated -> {
                    ArchiveVO archiveVO = new ArchiveVO();
                    // 发布时间
                    archiveVO.setDate(DateUtil.formatDate(retreated.getKey()));
                    // 数量
                    archiveVO.setCount(retreated.getValue().size());
                    // 文章
                    archiveVO.setArticles(retreated.getValue());
                    return archiveVO;
                })
                .collect(Collectors.toList());
        log.debug("Exit getArchives method");
        return archives;
    }
}
